package wicket.example.components.captcha;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import jj.play.ns.nl.captcha.Captcha;
import jj.play.ns.nl.captcha.Captcha.Builder;

import org.apache.log4j.Logger;

public class CaptchaGenerator {

	private static final Logger LOG = Logger.getLogger(CaptchaGenerator.class);

	public static Captcha getCapthca() {
		return new Builder(150, 50).addText().addBackground().addNoise()
				.addBorder().addNoise().addNoise().build();
	}

	public static byte[] getImage(Captcha captcha) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] imageInByte = null;
		try {
			ImageIO.write(captcha.getImage(), "jpg", baos);
			baos.flush();
			imageInByte = baos.toByteArray();
			baos.close();
		} catch (IOException e) {
			LOG.error("Error while trying to write image into byte array", e);
		}
		return imageInByte;
	}

}
